package com.dfrz.household.controller;

import com.dfrz.household.bean.Company;
import com.dfrz.household.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
public class CompanyController {
    @Autowired
    ICompanyService  companyService;
    @RequestMapping("/tocompany")
    public ModelAndView toCompanyView(){
        List<Company>list1=companyService.getAllCompany();
        int count=list1.size()/4;
        List<Company>list=companyService.getCompaniesByPage(1,4);
        ModelAndView mv=new ModelAndView();
        mv.addObject("companies",list);
        mv.addObject("count",count);
        mv.setViewName("company");
        return mv;
    }
    @RequestMapping("/tocompany/{pageNo}")
    public ModelAndView toCompanyView(@PathVariable("pageNo") Integer pageNo){
        List<Company>list1=companyService.getAllCompany();
        int count=list1.size()/4;
        List<Company>list=companyService.getCompaniesByPage(pageNo,4);
        ModelAndView mv=new ModelAndView();
        mv.addObject("companies",list);
        mv.addObject("count",count);
        mv.setViewName("company");
        return mv;
    }
    @RequestMapping("/toCompanyid/{id}")
    public ModelAndView toCompanyId(@PathVariable Integer id){
        Company company = companyService.getOneCompanyById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("company",company);
        mv.setViewName("companyid");

        return mv;
    }
}
